找传奇、传世资源到传世资源站!

经典贪吃蛇游戏(有源代码)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

C 经典贪吃蛇小游戏【有源代码】                    彩色界面,无需图形库! 经典贪吃蛇游戏(有源代码) C/C++游戏开发-第1张
等。。。【许多情况】
int main(){int olddirection='w'; //初始键盘信号int newdirection =0;//获取键盘信号init();//初始化 setmouth(60, 0);setcolour(0xF0);printf("\n得分:");printf("%d\n", snacklength - 3);printf("请切换为英文输入法\n");printf("W A S D键分别表示:上 左 下 右\n");while (1) {if (_kbhit()) { //检测缓冲区中是否有数据newdirection = tolower(_getch()); //将缓冲区中的数据以字符的形式读出if (newdirection == 27) {break;}else {//获取小蛇新的移动方向newdirection=movedire(olddirection, newdirection);if (newdirection != olddirection){olddirection = newdirection;}}}screenRefresh(newdirection);}return 0;}//更新得分void updategoal(){setcolour(0xF0);setmouth(60, 0);printf("\n得分:%d", snacklength - 3);if(snacklength==33){MessageBox(NULL,"恭喜你,成功得到“探花”称号","恭喜你,成功得到“探花”称号",MB_OK);}if(snacklength==53){MessageBox(NULL,"恭喜你,成功得到“榜眼”称号","恭喜你,成功得到“榜眼”称号",MB_OK);}if(snacklength==103){MessageBox(NULL,"恭喜你,成功得到“状元”称号","恭喜你,成功得到“状元”称号",MB_OK);}if(snacklength==153){MessageBox(NULL,"恭喜你,欢迎再来游玩","恭喜你,欢迎再来游玩",MB_OK);exit(0); }}//碰撞检测int checksnack(int newdirection){switch (newdirection){case 'w':if ((show[SnackSite[snackhead].x - 1][SnackSite[snackhead].y].kind)==1){snackdie();return 0;}else if((show[SnackSite[snackhead].x - 1][SnackSite[snackhead].y].kind) == 4){snackdie();return 0;}else if ((show[SnackSite[snackhead].x - 1][SnackSite[snackhead].y].kind) == 3){return 1;}else {return 2; }break;case 's':if ((show[SnackSite[snackhead].x 1][SnackSite[snackhead].y].kind) == 1){snackdie();return 0;}else if ((show[SnackSite[snackhead].x 1][SnackSite[snackhead].y].kind) == 4){snackdie();return 0;}else if ((show[SnackSite[snackhead].x 1][SnackSite[snackhead].y].kind) == 3){return 1;}else {return 2;}break;case 'a':if ((show[SnackSite[snackhead].x][SnackSite[snackhead].y-1].kind) == 1){snackdie();return 0;}else if ((show[SnackSite[snackhead].x][SnackSite[snackhead].y-1].kind) == 4){snackdie();return 0;}else if ((show[SnackSite[snackhead].x][SnackSite[snackhead].y-1].kind) == 3){return 1;}else {return 2;}break;case 'd':if ((show[SnackSite[snackhead].x][SnackSite[snackhead].y 1].kind) == 1){snackdie();return 0;}else if ((show[SnackSite[snackhead].x][SnackSite[snackhead].y 1].kind) == 4){snackdie();return 0;}else if ((show[SnackSite[snackhead].x][SnackSite[snackhead].y 1].kind) == 3){return 1;}else {return 2;}break; }} //创建新食物void newfood(){srand((unsigned)time(NULL));FoodSite.x = rand() % 38 1;FoodSite.y = rand() % 38 1;} //刷新屏幕void screenRefresh(int newdirection){int re = 0;int i = 0;re = checksnack(newdirection);snackmove(newdirection, re);background();show[FoodSite.x][FoodSite.y] = food;for (i = 0; i <= snackhead; i ){if (i == snackhead) {show[SnackSite[i].x][SnackSite[i].y] = snack;show[SnackSite[i].x][SnackSite[i].y].colour = 0x75; }else {show[SnackSite[i].x][SnackSite[i].y] = snack;}}showing();if (re == 1){snacklength ;updategoal();newfood();} }/*********************************************获取最新的方向**********************************************/int movedire(int olddirection, int newdirection){if (newdirection == 'w' || newdirection == 'a' || newdirection == 's' || newdirection == 'd'){ return newdirection;}else {return olddirection;} } /********************************** 展示最新的show*********************************/ void showing(){int line, lie;for (line = 0; line < MaxLine; line ){for (lie = 0; lie < MaxLie; lie ){setmouth(line, lie);setcolour(show[line][lie].colour);printf("%s",show[line][lie].display); }} } /******************************************小蛇死亡*******************************************/ void snackdie(){setmouth(20, 5);printf("Game over! 按任意键重新开始游戏!");getch();system("cls");int olddirection='w'; //初始键盘信号int newdirection =0;//获取键盘信号init();//初始化setmouth(60, 0);setcolour(0xF0);printf("\n得分:");printf("%d\n", snacklength - 3);printf("请切换为英文输入法\n");printf("W A S D键分别表示:上 左 下 右\n");while (1) {if (_kbhit()) { //检测缓冲区中是否有数据newdirection = tolower(_getch()); //将缓冲区中的数据以字符的形式读出if (newdirection == 27) {break;}else {//获取小蛇新的移动方向newdirection=movedire(olddirection, newdirection);if (newdirection != olddirection){olddirection = newdirection;}}}screenRefresh(newdirection);}} //小蛇移动void snackmove(int newdirection,int re){int i = 0;switch (newdirection){case 'w':for (i = 0; i < snackhead;i =1) {SnackSite[i].x = SnackSite[i 1].x;SnackSite[i].y = SnackSite[i 1].y;}SnackSite[snackhead].x -= 1;if (re == 1){snackhead = 1;SnackSite[snackhead].x = SnackSite[snackhead-1].x-1;SnackSite[snackhead].y = SnackSite[snackhead - 1].y; }break;case 's':for (i = 0; i < snackhead; i =1) {SnackSite[i].x = SnackSite[i 1].x;SnackSite[i].y = SnackSite[i 1].y;}SnackSite[snackhead].x = 1;if (re == 1){snackhead = 1;SnackSite[snackhead].x = SnackSite[snackhead - 1].x 1;SnackSite[snackhead].y = SnackSite[snackhead - 1].y; }break;case 'a':for (i = 0; i < snackhead; i =1) {SnackSite[i].x = SnackSite[i 1].x;SnackSite[i].y = SnackSite[i 1].y;}SnackSite[snackhead].y -= 1;if (re == 1){snackhead = 1;SnackSite[snackhead].x = SnackSite[snackhead - 1].x ;SnackSite[snackhead].y = SnackSite[snackhead - 1].y-1; }break;case 'd':for (i = 0; i < snackhead; i =1) {SnackSite[i].x = SnackSite[i 1].x;SnackSite[i].y = SnackSite[i 1].y;}SnackSite[snackhead].y = 1;if (re == 1){snackhead = 1;SnackSite[snackhead].x = SnackSite[snackhead - 1].x;SnackSite[snackhead].y = SnackSite[snackhead - 1].y 1; }break; } } //初始食物 void startfood(){FoodSite.x = 5;FoodSite.y = 10;show[5][10] = food;}//初始小蛇 void startsnack(){SnackSite[0].x = 35;SnackSite[0].y = 13;SnackSite[1].x = 35;SnackSite[1].y = 12;SnackSite[2].x = 35;SnackSite[2].y = 11;show[35][11] = snack;show[35][11].colour = 0x75;show[35][12] = snack;show[35][13] = snack;} /*******************************初始化生成show结构体数组*******************************/ void init(){//设置窗口大小system("mode con:cols=80 lines=43");//隐藏光标hidemouth();//生成背景图background();//创建初始食物startfood();//创建初始小蛇蛇体startsnack();//显示showing();} /**************************************生成背景图**************************************/void background(){int line, lie;for (line = 0; line < MaxLine; line ){for (lie = 0; lie < MaxLie; lie ){setmouth(line, lie);if (lie == 0 || lie == MaxLie-1 || line == 0 || line == MaxLine-1) {show[line][lie] = wall;}else {show[line][lie] = livearea;}}}} /********************************定义光标位置*********************************/ void setmouth(int x, int y){COORD coord;coord.X = y*2; //第3列coord.Y = x; //第3行//获取控制台缓冲区句柄HANDLE ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);//设置光标位置SetConsoleCursorPosition(ConsoleHandle, coord);} //设置种类的颜色void setcolour(int a){HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(hConsole, a);} //隐藏光标void hidemouth(){HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO CursorInfo;GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息CursorInfo.bVisible = 0; //隐藏控制台光标SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态 }

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复